Assets = {
    Asset('ANIM', 'anim/Hearthstone.zip'),
    Asset('IMAGE', 'images/inventoryimages/npcdebugstone.tex'),
    Asset('ATLAS', 'images/inventoryimages/npcdebugstone.xml'),

}
-- require "prefabs/telebase"

local function fn(Sim)
    local inst = CreateEntity()
    
    local function OnLoad(inst, data)
       
    end

    local function OnSave(inst, data)

    end

    local function InCave()
        --return GetWorld():IsCave()
        if TheWorld:HasTag('cave') then
            return true
        else
            return false
        end
    end

    local function eaterCheck_ret_foodtype(doer)
        -- make sure wathgrithr can eat this stone
        if doer.prefab == 'wathgrithr' then
            return 'MEAT'
        else
            --return "MEAT"
            return 'GENERIC'
        end
    end

    ----------------------------------------------------------------------------------------------------
    
  

    -----------------------------------------------------------------------------------------------------

    local function NPC_TASK_SINGLE_inList(doer)
        doer.IsDebugging = true
        if doer.NPC_SPAWN_LIB.NPC_List == nil then
            doer.NPC_SPAWN_LIB:NPC_LIST_INIT(doer)
        end

        local tempLists = doer.NPC_SPAWN_LIB.NPC_List
        if doer.IsDebugging_num == nil then
            doer.IsDebugging_num = #tempLists
        end

        if doer.IsDebugging_num == 0 then
            doer.IsDebugging_num = #tempLists
        end

        local currentCMD = tempLists[doer.IsDebugging_num]
        currentCMD.doer = doer
        doer.NPC_BASE_LIB:CreateNpcNearby2(currentCMD)
        doer.IsDebugging_num = doer.IsDebugging_num - 1

    end
    --------------------------------------------------------------------------------------------------------------------------------------------


    local function UseEQ_test(player)
        local tarItem = nil
        for k , item in pairs(player.components.inventory.equipslots) do
            -- if item then
            --     print(k)
            --     print(item)
            -- end
            if tostring(k) == "head" then
                tarItem = item
                break
            end
        end
        print(tarItem)
        -- player.components.playercontroller:DoControllerInspectItemFromInvTile(tarItem)
        tarItem.components.useableitem:StartUsingItem()

    end


    --------------------------------------------------------------------------------------------------------------------------------------------
    local function ActionTest(doer)
        -- -- 
        -- doer.AnimState:PlayAnimation("punch")
        -- doer.AnimState:PlayAnimation("shock")
        local animList_emoji = {
            "emote_sleepy",
            "emote_yawn",
            "emote_swoon",
            "emoteXL_loop_dance6",
            "emoteXL_loop_dance8",
            "emoteXL_loop_dance7",
            "emote_fistshake",
            "emote_flex",
            "emote_impatient",
            "emote_jumpcheer",
            "emote_laugh",
            "emote_shrug",
            "emote_slowclap",
            "emote_loop_carol",
            "idle_onemanband1_loop",
            "idle_onemanband2_loop",
            "run_loop",
            "run_pre",
            "run_pst",
            "mime2",
            "mime3",
            "mime4",
            "mime5",
            "mime6",
            "mime7",
            "mime8",
            "sleep_loop",
            "powerup",
            "powerdown",
            "shock",
            "death",
            "wakeup",
            "distress_loop"
        }
        local animList2 = {
		"bedroll_wakeup",
        "wakeup",
        "powerup",
        "powerdown",
        "fall_off",
        "transform_pre",
        "transform_pst",
        "weremoose_transform",
        "weremoose_revert",
        "shock",
        "rebirth",
        "death",
        "death2",
        "hungry",
        "idle_inaction_sanity",
        "idle_inaction_lunacy",
        "idle_groggy01_pre",
        "idle_inaction",
        "idle_wes",
        "bow_pre",
        "bow_loop",
        "bow_pst",
        "bellow",
        
        }
        ---------------------------------------------------------------------------
        local animList = {"staff_pre","staff_pre","chop_loop","frozen"}
        
        if doer.currentAnim == nil then
            doer.currentAnim = 1
        end

        doer.AnimState:PlayAnimation(animList[doer.currentAnim])
        print("Anim : "..animList[doer.currentAnim])
        doer.currentAnim = doer.currentAnim + 1

        if doer.currentAnim > #animList then
            doer.currentAnim = 1
        end

        -- doer.AnimState:PlayAnimation("atk_pre")
        -- doer.AnimState:PushAnimation("atk", false)

    end

    local function ActionTest2(doer)
        doer.AnimState:PlayAnimation("staff_pre")
        doer.AnimState:PushAnimation("staff", false)

    end
    --------------------------------------------------------------------------------------------------------------------------------------------

    local function EventTest(doer)
        -- for eventName,tempTable in pairs(doer.event_listening) do
        --     print(eventName)
        --     print(tempTable)
        --     print("----------")
        -- end
        function doer:testEventFunction2()
            print("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF  "..math.random(10))

        end
        function doer:testEventFunction()
            print("LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL  "..math.random(10))

        end

        if doer.even_test_flag ~= true then
            doer:RemoveEventCallback("event_test",doer.testEventFunction)
            doer:ListenForEvent("event_test",doer.testEventFunction2)
            doer.even_test_flag = true
            print(" add event ")
        else
            -- inst:RemoveEventCallback("")
            -- inst:ListenForEvent("death", v.scene_killedfn, v)
            -- inst:RemoveEventCallback("death", enemy.scene_killedfn, enemy)
            doer:RemoveEventCallback("event_test",doer.testEventFunction2)
            doer:ListenForEvent("event_test",  doer.testEventFunction )
  

            doer.even_test_flag = false
            print(" try to remove event")

        end
        
    end

    --------------------------------------------------------------------------------------------------------------------------------------------
    local function try2remove(doer)
        local x ,y,z = doer.Transform:GetWorldPosition()
        local targetItems = TheSim:FindEntities(x, y, z, 2,nil,{"wall"},nil)
        for k, item in pairs(targetItems) do
            print(item)
        end
    end
    -----------------------------
    local function ScanAround(doer,range)
        local x ,y, z = doer.Transform:GetWorldPosition()
        print("----------------------------------------------------------------------------")
        local targetInsts = TheSim:FindEntities(x, y, z, range or 10,nil,{"wall"},nil)
        for k, tempInst in pairs(targetInsts) do
            print(tempInst,tostring(tempInst.prefab))
        end
        print("----------------------------------------------------------------------------")
    end

    local function weapon_test(doer)
        local function EquipWeapon(inst)
            if inst.components.inventory and not inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS) then
                local weapon = CreateEntity()
                --[[Non-networked entity]]
                weapon.entity:AddTransform()
                weapon:AddComponent("weapon")
                weapon.components.weapon:SetDamage(inst.components.combat.defaultdamage)
                weapon.components.weapon:SetRange(inst.components.combat.attackrange, inst.components.combat.attackrange+4)
                weapon.components.weapon:SetProjectile("eye_charge")
                weapon:AddComponent("inventoryitem")
                weapon.persists = false
                weapon.components.inventoryitem:SetOnDroppedFn(weapon.Remove)
                weapon:AddComponent("equippable")
        
                inst.components.inventory:Equip(weapon)
            end
        end

        local function getWeapon()

            local weapon = SpawnPrefab('cane')
            --[[Non-networked entity]]
            -- weapon.entity:AddTransform()
            if weapon.components.weapon == nil then
                weapon:AddComponent("weapon")
            end
            weapon.components.weapon:SetDamage(1)
            weapon.components.weapon:SetRange(30)
            weapon.components.weapon:SetProjectile("eye_charge")
            -- weapon.components.weapon:SetProjectile("spore_moon")
            -- weapon:AddComponent("inventoryitem")
            weapon.persists = false
            -- weapon.components.inventoryitem:SetOnDroppedFn(weapon.Remove)
            -- weapon:AddComponent("equippable")
    
            return weapon

        end
        doer.components.inventory:GiveItem(getWeapon())
    end


    local function colour(doer)
        -- if doer.colour_test ~= true then
        --     doer.colour_test = true
        --     local r,g,b,a =  doer.AnimState:GetMultColour()
        --     if doer.rgba == nil then
        --         doer.rgba = {r=r,g=g,b=b,a=a}
        --     end

        --     if doer.rgba.a ~= a then
        --         doer.AnimState.SetMultColour(r,g,b,a)
        --     end

        -- else
        --     doer.colour_test = false
        --     doer.AnimState:SetMultColour(255, 255, 255, .4)

        -- end

        if doer.colour_test ~= true then
            doer.colour_test = true
            doer.AnimState:SetMultColour(0, 0, 0, .4)
        else
            doer.colour_test = false
            doer.AnimState:SetMultColour(255, 255, 255, 1)
        end
    end

    local function random_test(doer)
        if doer.NPC_SPAWN_LIB.NPC_List == nil then
            doer.NPC_SPAWN_LIB:NPC_LIST_INIT(doer)
        end

        print("TUNING.NPC_Rewards_Less",TUNING.NPC_Rewards_Less)
        print("TUNING.NPC_Spawn_away_from_the_camp",TUNING.NPC_Spawn_away_from_the_camp)
        print("TUNING.NPC_Appearing_probability",TUNING.NPC_Appearing_probability)
        -- print("TUNING.NPC_Rewards_Less",TUNING.NPC_Rewards_Less)
        local listNum = #doer.NPC_SPAWN_LIB.NPC_List
        for i = 1, listNum, 1 do
            -- doer.NPC_SPAWN_LIB.NPC_List[i].Probability = 10
            print(doer.NPC_SPAWN_LIB.NPC_List[i].debuffname,doer.NPC_SPAWN_LIB.NPC_List[i].Probability)
        end
    end

    local function OneActionTest(doer)
        local function theAction(doer)
            if not doer.NPC_ACTIONS_LIB:after_OpenGiftBox(doer,10) then
                doer:DoTaskInTime(0.5,function() theAction(doer) end)
            else
                print("Action End")
                return
            end
        end

        doer:DoTaskInTime(1,function() theAction(doer) end)
        
    end
    local function SE_colour_test(doer)
        local se = SpawnPrefab("pocketwatch_recall_marker")
        se.Transform:SetPosition(doer.Transform:GetWorldPosition())
        
        if doer.SE_test == nil then
            doer.SE_test = 0
        end

        if doer.SE_test == 0 then
            se:AddComponent("colouradder")
            se.components.colouradder:OnSetColour(255,255,255,1)
        elseif doer.SE_test == 1 then
            se:AddComponent("colouradder")
            se.components.colouradder:OnSetColour(0,0,0,1)
        elseif doer.SE_test == 2 then
            se:AddComponent("colouradder")
            se.AnimState:SetMultColour(0, 0, 0, 1) --- black colour
        elseif doer.SE_test == 3 then
            se.AnimState:SetMultColour(255, 255, 255, 1)
        end
        doer.SE_test = doer.SE_test + 1
    end

    local function ArrowTest_old(doer)
        local function SetArrowLocation(ArrowInst,BasePoint,TargetPoint)
            -- local tarPoint = doer.ArrowPoint
			local angle = doer:GetAngleToPoint(TargetPoint.x,TargetPoint.y,TargetPoint.z)
            -- local radius = -3
            local radius = 0.5
            local theta = (angle)*DEGREES
            local offset = Vector3(radius * math.cos( theta ), 0, -radius * math.sin( theta ))
            ArrowInst.Transform:SetPosition(BasePoint.x + offset.x,BasePoint.y,BasePoint.z + offset.z)
            ArrowInst:PushEvent("PointingTo",{tarPoint=TargetPoint})
        end

        local currPoint = Vector3(doer.Transform:GetWorldPosition())
        if doer.ArrowPoint == nil then
            doer.ArrowPoint = currPoint
            print("GetCurrentPoint")
            return
        end

        if doer.TheArrow then
            -- doer.TheArrow:Remove()
            -- doer.TheArrow:
            SetArrowLocation(doer.TheArrow, currPoint, doer.ArrowPoint)
            if doer.ArrowTestNum == nil then
                doer.ArrowTestNum = 0
            end
            doer.ArrowTestNum = doer.ArrowTestNum + 1
            if doer.ArrowTestNum > 5 then
                doer.ArrowPoint = nil
                doer.ArrowTestNum = 0
                doer.TheArrow:Remove()
                doer.TheArrow = nil
            end
        else
            local tarPoint = doer.ArrowPoint

            local ArrowInst = SpawnPrefab("arrow_npc_event")
            SetArrowLocation(ArrowInst, currPoint, tarPoint)
            doer.TheArrow = ArrowInst
        end
    end

    local function ArrowTest(doer,baseInst)
        -- local currPoint = Vector3(doer.Transform:GetWorldPosition())
        if doer.ArrowInst == nil then
            local ArrowInst = SpawnPrefab("arrow_npc_event")
            ArrowInst.Transform:SetPosition(doer.Transform:GetWorldPosition())
            if baseInst then
                ArrowInst.components.com_npc_arrow.BasePintInst = baseInst
            else
                ArrowInst.components.com_npc_arrow.BasePintInst = doer
            end

            doer.ArrowInst = ArrowInst

            local pigman = SpawnPrefab("pigman")
            pigman.Transform:SetPosition(doer.Transform:GetWorldPosition())
            ArrowInst.components.com_npc_arrow.tarInst = pigman

        else
            if doer.ArrowInst then
                doer.ArrowInst:Remove()
            end
            doer.ArrowInst = nil
        end

    end

    local function Tabes_compare_Test(doer)
        if doer.testlfag ~= true then
            doer.testlfag = true
            local pigman = SpawnPrefab("pigman")
            pigman.Transform:SetPosition(doer.Transform:GetWorldPosition())
            doer.tempPig = pigman
            local tempTable1 = {}
            for k, v in pairs(pigman) do
                tempTable1[k] = tostring(v)
            end
            doer.tempTable1 = tempTable1
            doer:DoTaskInTime(2,function() pigman.components.health:Kill()  end)

        else
            -- doer.Transform:SetPosition(doer.tempPig.Transform:GetWorldPosition())
            print("------------------------------------------")
            for k, v in pairs(doer.tempPig) do
                if doer.tempTable1[k] ~= tostring(v) then
                    print("+++++++++++++++++")
                    print(tostring(k).."       "..tostring(doer.tempTable1[k]) .. "          "..tostring(v) )
                    print("+++++++++++++++++")
                end
            end
            print("------------------------------------------")
        end
    end
    local function onEat(inst, doer)
        doer.IsDebugging = true

        local player = doer
        -- player.components.temperature:SetTemperature(30)
        -- doer.components.combat.externaldamagemultipliers._modifier = doer.components.combat.externaldamagemultipliers._modifier + 0.5
        print('OnEat:')
        local prefabName = SpawnPrefab('npcdebugstone')
        doer.components.inventory:GiveItem(prefabName) -- give a new stone


        if doer.components.debuffable:HasDebuff("npcdebuff_inplayer") == false then
			doer.components.debuffable:AddDebuff("npcdebuff_inplayer", "npcdebuff_inplayer")
		end

        doer.NPC_BASE_LIB:ProtectingNPCs(doer)
        -- if doer.components.debuffable:HasDebuff("npcdebuff_debugtest") == false then
		-- 	doer.components.debuffable:AddDebuff("npcdebuff_debugtest", "npcdebuff_debugtest")
		-- end

        --------------------------------------------------------------------------------------------------------------------------------------------------------
        -- ScanAround(doer,5)
        -- weapon_test(doer)
        -- doer.NPC_SPAWN_LIB:SPAWN_NPC_BY_PROBABILITY(doer)
        -- NPC_TASK_SINGLE_inList(doer)
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 450, debuffname = "npcdebuff_bushhat"})
        ----------------------------------------------------------------------------------------------
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 450, debuffname = "npcdebuff_wolfgang_and_horn",NPC_NAME="wolfgang"})
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 450,NPC_NAME="waxwell",debuffname="npcdebuff_maxwell_and_shadow"})
        doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 450, NPC_NAME="webber",debuffname="NpcDebuff_Webber_and_spiders"})
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 450, NPC_NAME="walrus_npc",debuffname="npcdebuff_walrus_team"})
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 450, NPC_NAME="carnival_host_npc",debuffname = "npcdebuff_shadowcrow" ,SpawnDistance = 10})
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 450, debuffname = "npcdebuff_butterfly" })
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 450, })
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 450, NPC_NAME="carnival_host_npc",debuffname = "npcdebuff_shadowcrow" ,SpawnPoint = Vector3(0,40,0)})
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 120, NPC_NAME="pigguard",debuffname = "npcdebuff_pigman_arena"})
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 120, NPC_NAME="wes",debuffname = "npcdebuff_wes_and_balloons"})
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 450, debuffname = "npcdebuff_give_propsign"})
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,NPC_NAME = "player",debuffname = "npcdebuff_crafty_fishbowl" })
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 450, debuffname = "npcdebuff_butterfly"})
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 450, NPC_NAME="woodie",debuffname = "npcdebuff_woodie_beaver"})
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 450,NPC_NAME ={"woodie","wilson"} ,debuffname = "npcdebuff_special_items"})
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 450, debuffname = "npcdebuff_penguins"})
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,NPC_NAME = "player",debuffname = "npcdebuff_clockwork_guard" })
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,time = 450,NPC_NAME = {"wilson","wolfgang","woodie","webber"},debuffname = "npcdebuff_shave_beards" })
        -- doer.NPC_BASE_LIB:CreateNpcNearby2({doer = doer,NPC_NAME = "player",debuffname = "npcdebuff_butterfly_staff" })
        

        -- SE_colour_test(doer)
        -- Falldown_test(doer)

        -- doer:Hide()
        -- doer.DynamicShadow:SetSize(0, 0)
        -- doer:DoTaskInTime(10,function() doer:Show() doer.DynamicShadow:SetSize(1.3, .6) end)
        ----------------------------------------------------------------------------------------------

        -- doer:DoTaskInTime(1,function()
        --     OneActionTest(doer)
        -- end)
        -- doer:AddDebuff("npcdebuff_debug_test","npcdebuff_debug_test")


        -- local test = require("prefabs/test")
        -- local Fx = test()
        -- Fx:MainTest(doer)

        -- ArrowTest(doer,prefabName)
        -- Tabes_compare_Test(doer)

        -- doer:AddDebuff("npcdebuff_debug_test","npcdebuff_debug_test")
    end


    local function onCook(inst, cooking_device, doer)        
        doer.NPC_TaskPoint = Vector3(doer.Transform:GetWorldPosition())        
        print(doer.NPC_TaskPoint.x)
        print(doer.NPC_TaskPoint.y)
        print(doer.NPC_TaskPoint.z)
        print('OnCOOK:')
    end

    -----------------------------------------------------------------------------------------------
    local function onTest(inst)
        local tempUserID = inst.components.inventoryitem.owner.userid
    end

    -----------------------------------------------------------------------------------------------


    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()
    inst.entity:SetPristine()

    MakeInventoryPhysics(inst)
    MakeHauntableLaunch(inst)

    -- anim:SetBank('Hearthstone')
    -- anim:SetBuild('Hearthstone')
    -- anim:PlayAnimation('idle')

    -- inst.AnimState:SetBank("bat_tree_fx")
    -- inst.AnimState:SetBuild("bat_tree_fx")
    inst.AnimState:SetBank("fishbowl")
    inst.AnimState:SetBuild("hutch_fishbowl")
    inst.AnimState:PlayAnimation("idle_loop", true)


    if not TheWorld.ismastersim then
        return inst
    end

    inst.fishAlive = "hutch_fishbowl"
    inst.fishDead = "hutch_fishbowl_dead"
    inst.isFishAlive = true

    inst:AddComponent('inventoryitem')
    -- inst.components.inventoryitem:ChangeImageName("halloweenpotion_sparks")
    inst.components.inventoryitem:ChangeImageName(inst.fishAlive)
    -- inst.components.inventoryitem:SetSinks(true)
    -- inst.components.inventoryitem.imagename = 'npcdebugstone'
    -- inst.components.inventoryitem.atlasname = 'images/inventoryimages/npcdebugstone.xml'
    inst.components.inventoryitem:SetOnPutInInventoryFn(onTest)

    inst:AddComponent('edible')
    inst:AddTag('edible')
    inst.components.edible.foodtype = 'GENERIC'
    inst.components.edible.healthvalue = 2000
    inst.components.edible.hungervalue = 2000
    inst.components.edible.sanityvalue = 2000
    inst.components.edible.oneaten = onEat

    inst:AddComponent('cookable')
    inst.components.cookable.product = 'npcdebugstone'
    inst.components.cookable.oncooked = onCook

    -- inst.OnLoad = OnLoad
    -- inst.OnSave = OnSave

    return inst
end

return Prefab('common/inventory/npcdebugstone', fn, Assets)
